package com.xywh.composeLibrary.view

import androidx.compose.foundation.layout.*
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.xywh.composeLibrary.theme.LoadSirText
import com.xywh.composeLibrary.R

/**
 *
 * @param stateEnum StateEnum 状态类型
 * @param reloadOnClick Function0<Unit> 数据为空，错误状态时，点击事件
 * @param content [@androidx.compose.runtime.Composable] Function0<Unit> 列表数据item布局
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun XxLoadSir(
    stateEnum: StateEnum = StateEnum.EMPTY,
    reloadOnClick: () -> Unit,
    content: @Composable () -> Unit
) {
    val onClickEnable = stateEnum != StateEnum.LOADING && stateEnum != StateEnum.SUCCESS
    Surface(
        enabled = onClickEnable,
        onClick = {
            reloadOnClick.invoke()
        }) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .fillMaxHeight(),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally,

            ) {

            val iconId: Int
            val textDesc: String
            when (stateEnum) {
                StateEnum.LOADING -> {
                    textDesc = "数据加载中..."
                    StateLoadingView(textDesc)
                }

                StateEnum.EMPTY -> {
                    iconId = R.drawable.xx_load_empty
                    textDesc = "暂无数据"
                    StateView(iconId, textDesc)
                }

                StateEnum.ERROR -> {
                    iconId = R.drawable.xx_load_error
                    textDesc = "数据获取失败\n请检查网络后点击重新加载"
                    StateView(iconId, textDesc)
                }

                else -> {
                    content()
                }
            }


        }
    }

}

/**
 *
 * @param iconId Int 状态图片id
 * @param textDesc String 文字描述
 */
@Composable
private fun StateView(iconId: Int, textDesc: String) {

    Icon(
        painter = painterResource(id = iconId),
        contentDescription = "数据为不同状态时显示的图片",
        tint = LoadSirText
    )
    StateTextView(textDesc)

}

/**
 *
 * @param textDesc String 文字描述
 */
@Composable
private fun StateLoadingView(textDesc: String) {
    CircularProgressIndicator(color = MaterialTheme.colorScheme.primary)
    StateTextView(textDesc)
}

/**
 *
 * @param textDesc String 文字描述
 */
@Composable
private fun StateTextView(textDesc: String) {
    Text(
        modifier = Modifier.padding(vertical = 10.dp),
        text = textDesc,
        color = LoadSirText,
        textAlign = TextAlign.Center,
        style = TextStyle(
            fontWeight = FontWeight.W500, //设置字体粗细
            fontSize = 14.sp,
            letterSpacing = 2.sp,
            lineHeight = 20.sp
        )
    )
}


enum class StateEnum {
    LOADING, //加载中
    EMPTY, //数据为空
    ERROR, //错误
    SUCCESS //成功
}

